package com.atguigu.tingshu.common.util;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * @author xiongkunYang
 */
public final class MD5 {


    private static final char[] HEX_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
            '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String encrypt(String strSrc) {
        try {
            byte[] inputBytes = strSrc.getBytes(StandardCharsets.UTF_8);
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(inputBytes);
            byte[] digestBytes = md.digest();

            int length = digestBytes.length;
            char[] chars = new char[length * 2];
            int charIndex = 0;

            for (byte b : digestBytes) {
                chars[charIndex++] = HEX_CHARS[(b >>> 4) & 0xf];
                chars[charIndex++] = HEX_CHARS[b & 0xf];
            }

            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5加密出错：" + e.getMessage(), e);
        }
    }

    public static void main(String[] args) {
        String msg = "{\"liveRoomId\":1,\"msgContent\":1,\"msgType\":\"4\",\"timestamp\":1679969941853}";
        System.out.println(msg);
        msg = msg.replaceAll("\\\\", "");
        System.out.println(msg);
        System.out.println(MD5.encrypt("study well and progress every day"));
    }
}
